<?php

/*
 *    author:           Kyle Gadd
 *    documentation:    http://www.php-ease.com/classes/blueprint-css.html
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

class Blueprint {

  private $container; // whether or not a container div is open
  private $col; // whether or not a column is open
  private $rows; // the total number of rows
  private $width; // used in $this->page()
  private $colors = array('border:2px solid #3F80BA; background-color:#538EC3; color:#FFFFFF;', 'border:2px solid #13B322; background-color:#64CD6E; color:#FFFFFF;', 'border:2px solid #990000; background-color:#FFDD00; color:#000000;', 'border:2px solid #24003F; background-color:#63639B; color:#FFFFFF;', 'border:2px solid #FF8C00; background-color:#FBE858; color:#233E8E;', 'border:2px solid #BE3A3E; background-color:#C64F52; color:#FFFFFF;', 'border:2px solid #886644; background-color:#CC9D59; color:#000000;', 'border:2px solid #CCCCCC; background-color:#DEDEDE; color:#000000;', 'border:2px solid #83A8CC; background-color:#F5F9FE; color:#003366;', 'border:2px solid #666666; background-color:#333333; color:#FFFFFF;', 'border:2px solid #AABBDD; background-color:#000033; color:#FFAA33;', 'border:2px solid #1B7E6F; background-color:#1A287A; color:#D1C923;', 'border:2px solid #FFDD33; background-color:#FAFAFA; color:#E01900;', 'border:2px solid #3D2500; background-color:#EE8800; color:#FFFFFF;', 'border:2px solid #1F563C; background-color:#53C64E; color:#FBFF24;'); // used in $this->div()

  function __construct ($width=950) {
    list($rows, $width) = $this->width($width);
    $this->rows = $rows;
    $this->width = $width;
  }

  public function page ($showgrid=false) {
    $html = '<div class="container';
    if ($showgrid) $html .= ' showgrid';
    $html .= '"';
    if ($this->width != 950) $html .= ' style="width:' . $this->width . 'px;"';
    $html .= '>';
    $this->container = '</div>';
    return $html;
  }

  public function row ($content='') {
    return $this->col($this->rows, 'last', $content);
  }
  
  public function col ($class, $last='', $content='') {
    if (is_numeric($class)) {
      $class = 'span-' . $class;
    } elseif (is_array($class)) {
      $vars = array();
      foreach ($class as $key => $value) $vars[] = (is_numeric($key)) ? $value : $key . '-' . $value;
      $class = implode(' ', $vars);
    }
    if ($last == 'last' || $last === true) {
      $class .= ' last';
    } elseif (!empty($last)) {
      $content = $last . $content;
    }
    $html = $this->col . "\n  " . '<div class="' . $class . '">' . $content;
    $this->col = '</div>';
    return $html;
  }
 
  public function swap ($col1, $col2, $content1, $content2) {
    $html = '';
    if (($col1 + $col2) != $this->rows) return $html;
    $html .= "\n    " . '<div class="span-' . $col1 . ' prepend-' . $col2 . '">' . $content1 . '</div>';
    $html .= "\n    " . '<div class="span-' . $col2 . ' pull-' . $this->rows . ' last">' . $content2 . '</div>';
    return $this->row($html);
  }
  
  public function double ($col1, $col2, $content1, $content2) {
    $html = '';
    if (($col1 + $col2) != $this->rows) return $html;
    $html .= "\n    " . '<div class="span-' . $col1 . '">' . $content1 . '</div>';
    $html .= "\n    " . '<div class="span-' . $col2 . ' last">' . $content2 . '</div>';
    return $this->row($html);
  }
  
  public function triple ($col1, $col2, $col3, $content1, $content2, $content3) {
    $html = '';
    if (($col1 + $col2 + $col3) != $this->rows) return $html;
    $html .= "\n    " . '<div class="span-' . $col1 . '">' . $content1 . '</div>';
    $html .= "\n    " . '<div class="span-' . $col2 . '">' . $content2 . '</div>';
    $html .= "\n    " . '<div class="span-' . $col3 . ' last">' . $content3 . '</div>';
    return $this->row($html);
  }
  
  public function quad ($col1, $col2, $col3, $col4, $content1, $content2, $content3, $content4) {
    $html = '';
    if (($col1 + $col2 + $col3 + $col4) != $this->rows) return $html;
    $html .= "\n    " . '<div class="span-' . $col1 . '">' . $content1 . '</div>';
    $html .= "\n    " . '<div class="span-' . $col2 . '">' . $content2 . '</div>';
    $html .= "\n    " . '<div class="span-' . $col3 . '">' . $content3 . '</div>';
    $html .= "\n    " . '<div class="span-' . $col4 . ' last">' . $content4 . '</div>';
    return $this->row($html);
  }
  
  public function close () {
    return $this->col . $this->container;
  }

  public function div ($text='', $filler='') {
    $dummy = array();
    $dummy[] = 'But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure? On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.';
    $dummy[] = 'The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators. To achieve this, it would be necessary to have uniform grammar, pronunciation and more common words.If several languages coalesce, the grammar of the resulting language is more simple and regular than that of the individual languages. The new common language will be more simple and regular than the existing European languages. It will be as simple as Occidental; in fact, it will be Occidental. To an English person, it will seem like simplified English, as a skeptical Cambridge friend of mine told me what Occidental is.';
    $dummy[] = 'A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine. I am so happy, my dear friend, so absorbed in the exquisite sense of mere tranquil existence, that I neglect my talents. I should be incapable of drawing a single stroke at the present moment; and yet I feel that I never was a greater artist than now. When, while the lovely valley teems with vapour around me, and the meridian sun strikes the upper surface of the impenetrable foliage of my trees, and but a few stray gleams steal into the inner sanctuary, I throw myself down among the tall grass by the trickling stream; and, as I lie close to the earth, a thousand unknown plants are noticed by me: when I hear the buzz of the little world among the stalks, and grow familiar with the countless indescribable forms of the insects and flies, then I feel the presence of the Almighty, who formed us in his own image, and the breath of that universal love which bears and sustains us, as it floats around us in an eternity of bliss; and then, my friend, when darkness overspreads my eyes, and heaven and earth seem to dwell in my soul and absorb its power, like the form of a beloved mistress, then I often think with longing, Oh, would I could describe these conceptions, could impress upon paper all that is living so full and warm within me, that it might be the mirror of my soul, as my soul is the mirror of the infinite God! O my friend -- but it is too much for my strength -- I sink under the weight of the splendour of these visions!';
    if (is_numeric($filler)) {
      $num = rand(0, 2);
      $string = $dummy[$num];
      if ($filler > strlen($string)) while ($filler > strlen($string)) $string .= ' ' . $string;
      $text .= '<br /><br />' . substr($string, 0, $filler);
    }
    $num = rand(0, count($this->colors)-1);
    $color = $this->colors[$num];
    unset($this->colors[$num]);
    shuffle($this->colors);
    return '<div style="padding:20px 10px; opacity:0.5; filter:alpha(opacity=50); ' . $color . '"><div style="opacity:1; filter:alpha(opacity=100);">' . $text . '</div></div>';
  }
  
  private function width ($num) {
    $widths = array(0, 30, 70, 110, 150, 190, 230, 270, 310, 350, 390, 430, 470, 510, 550, 590, 630, 670, 710, 750, 790, 830, 870, 910, 950);
    if ($num <= 24) return array($num, $widths[$num]);
    foreach ($widths as $rows => $width) {
      if ($num > $width) continue;
      if ($num <= $width) break;
    }
    return array($rows, $width);
  }

}

?>